package net.maku.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import net.maku.bean.model.MyException;

public class MyRestTemplate {
	
 
	    public String postData(String url, String jsonBody) {
	        RestTemplate restTemplate = new RestTemplate();
	        HttpHeaders headers = new HttpHeaders();
	        
	        headers.setContentType(MediaType.APPLICATION_JSON);
	        HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
	 
	        String result = restTemplate.postForObject(url, entity, String.class);
	 
	        return result;
	    }

	    
	    
	    public static String postData( int type ) {
	    	RestTemplate restTemplate = new RestTemplate();
	    	// 定义请求参数和url链接
	    	String url = "http://localhost:8080/api/test";
	    	if(type==1) {
	    		url = "http://52.77.234.251:8080/image_analyse";
	    	}
	    	MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
	    	
	    	String filePath = "/home/jishu/images/tx4.jpg";
	    	String base64_image  = fileToBase64( filePath );
	    	params.add("username", "admin");
	    	params.add("image_base64", base64_image );
	    	// 创建 HttpHeaders 对象，并设置 Content-Type 为 application/x-www-form-urlencoded
	    	HttpHeaders headers = new HttpHeaders();
	    	headers.setContentType(MediaType.APPLICATION_JSON);
	    	// 使用 MultiValueMap 类型创建请求体，并将参数放入其中
	    	HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
	    	// 调用 RestTemplate 方法进行请求
	    	ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
	    	// 获取请求结果并处理
	    	if (response.getStatusCode() == HttpStatus.OK) {
	    	    String result = response.getBody();
	    	    System.out.println( result );
	    	    // 处理请求结果
	    	    return result;
	    	}
	    	System.out.println("---------------------------------------------");
	    	
	    	return "";
	    }
 


	    public static String fileToBase64( String filePath) {
	    	try {
		    	ByteArrayOutputStream baos= new ByteArrayOutputStream();
		    	InputStream fis = new FileInputStream(filePath);
		    	byte[] buff = new byte[1024]; 
		    	int length = 0;
		    	while((length = fis.read(buff)) != -1){
		    		baos.write(buff, 0, length);
		        }
		    	fis.close();
				//System.out.println( baos.size() );
		    	//return MyBase64.enByteCode( baos.toByteArray() );
		    	return Base64.encodeBase64String(baos.toByteArray());
	    	} catch ( Exception e ) {
				e.printStackTrace();
				throw new MyException( -1, "生成base64错误" );
			}
	    	
	        
	    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//postData();

	}

}
